package com.mall.order.services;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mall.order.OrderQueryService;
import com.mall.order.constant.OrderRetCode;
import com.mall.order.converter.OrderConverter;
import com.mall.order.dal.entitys.*;
import com.mall.order.dal.persistence.OrderItemMapper;
import com.mall.order.dal.persistence.OrderMapper;
import com.mall.order.dal.persistence.OrderShippingMapper;
import com.mall.order.dto.*;
import com.mall.order.utils.ExceptionProcessorUtils;
import lombok.extern.slf4j.Slf4j;
import net.bytebuddy.asm.Advice;
import org.apache.dubbo.common.utils.CollectionUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

/**
 *  ciggar
 * create-date: 2019/7/30-上午10:04
 */
@Slf4j
@Component
@Service
public class OrderQueryServiceImpl implements OrderQueryService {

    @Autowired
    OrderMapper orderMapper;
    @Autowired
    OrderConverter orderConverter;
    @Autowired
    OrderItemMapper orderItemMapper;
    @Autowired
    OrderShippingMapper orderShippingMapper;
    @Override
    public OrderListResponse queryAllOrders(OrderListRequest orderListRequest) {
        OrderListResponse orderListResponse = new OrderListResponse();
        Long userId = orderListRequest.getUserId();
        Integer page = orderListRequest.getPage();
        Integer size = orderListRequest.getSize();
        String sort = orderListRequest.getSort();

        //根据userId查找
        Example example = new Example(Order.class);
        //?
        example.setOrderByClause(sort);
        example.createCriteria().andEqualTo("userId",userId);
        List<Order> orders = orderMapper.selectByExample(example);
        //封装成OrderDetailInfoList
        List<OrderDetailInfo> orderDetailInfos = orderConverter.order2DetailListDTO(orders);
        for (OrderDetailInfo order : orderDetailInfos) {
            //根据每个orderId查找itemListDto
            String orderId =order.getOrderId();
            Example orderItemExample = new Example(OrderItem.class);
            orderItemExample.createCriteria().andEqualTo("orderId", orderId);
            List<OrderItem> orderItems = orderItemMapper.selectByExample(orderItemExample);
            List<OrderItemDto> orderItemDtoList = orderConverter.item2dto(orderItems);
            order.setOrderItemDto(orderItemDtoList);
            //根据每个orderId查找OrderShippingDto
            Example shippingExample = new Example(OrderShipping.class);
            shippingExample.createCriteria().andEqualTo("orderId",orderId);
            OrderShipping orderShipping = orderShippingMapper.selectOneByExample(shippingExample);
            OrderShippingDto orderShippingDto = orderConverter.shipping2dto(orderShipping);
            order.setOrderShippingDto(orderShippingDto);
        }
        PageInfo<OrderDetailInfo> orderDetailInfoPageInfo = new PageInfo<>(orderDetailInfos);
        long total = orderDetailInfoPageInfo.getTotal();
        orderListResponse.setTotal(total);
        orderListResponse.setDetailInfoList(orderDetailInfos);
        return orderListResponse;
    }

    @Override
    public OrderResultVO orderQueryById(OrderDetailRequest request) {
        //result 内的bean
        OrderResultVO orderResultVO = new OrderResultVO();
        //goodList中的bean
        ArrayList<OrderItemDtoVO> goodsList = new ArrayList<>();
        ///1.参数基础检查
        request.requestCheck();
        //获取请求参数
        String orderId = request.getOrderId();

        List<OrderItem> orderItems = orderItemMapper.queryByOrderId(orderId);
        for (OrderItem orderItem : orderItems) {
            OrderItemDtoVO orderItemDtoVO = new OrderItemDtoVO();
            orderItemDtoVO.setId(orderItem.getId());
            orderItemDtoVO.setItemId(orderItem.getItemId());
            orderItemDtoVO.setOrderId(orderItem.getOrderId());
            orderItemDtoVO.setNum(orderItem.getNum());
            orderItemDtoVO.setTitle(orderItem.getTitle());
            orderItemDtoVO.setPrice(orderItem.getPrice());
            orderItemDtoVO.setTotalFee(orderItem.getTotalFee());
            orderItemDtoVO.setOrderStatus(orderItem.getStatus());
            orderItemDtoVO.setPicPath(orderItem.getPicPath());
            goodsList.add(orderItemDtoVO);
        }
        Order order = orderMapper.selectByPrimaryKey(orderId);
        Long userId = order.getUserId();
        orderResultVO.setUserId(userId);

        OrderShipping orderShipping = orderShippingMapper.selectByPrimaryKey(orderId);
        orderResultVO.setUserName(orderShipping.getReceiverName());
        orderResultVO.setTel(orderShipping.getReceiverPhone());
        orderResultVO.setStreetName(orderShipping.getReceiverAddress());

        Double orderTotal = new Double(0.00);
        for (OrderItemDtoVO  orderItemDtoVO : goodsList) {
            orderTotal = orderTotal + orderItemDtoVO.getTotalFee();
        }
        orderResultVO.setOrderTotal(orderTotal);
        orderResultVO.setGoodsList(goodsList);
        return orderResultVO;
    }
}
